package com.qf.controller;

import com.qf.entity.Emplyee;
import com.qf.entity.Permission;
import com.qf.entity.Role;
import com.qf.service.EmplyeeService;
import com.qf.utils.RedisUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class EmplyeeController {

    @Resource
    private EmplyeeService emplyeeService;

    @Resource
    private RedisUtil redisUtil;

    @GetMapping("/test")
    public String test(){
        return "this is test";
    }

    @PostMapping("/loginTel/{etel}/{code}")
    public String loginTel(@PathVariable String etel,@PathVariable String code ){
        System.out.println(etel+"\t"+code);
        Object o = redisUtil.get(etel);
        String c= (String) o;
        if(c.equals(code)){
            String ename = emplyeeService.getEnameByTel(etel);
            return ename;
        }
        return "fail";
    }

    @GetMapping("/login/{ename}/{epassword}")
    public String login(@PathVariable String ename,
                        @PathVariable String epassword){
        System.out.println(ename+epassword);
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(ename, epassword);
        try {
            subject.login(token);
           /* Emplyee emplyee = emplyeeService.login(ename, epassword);
            session.setAttribute("emplyee",emplyee);*/
            return "success";
        } catch (AuthenticationException e) {
            e.printStackTrace();
            return "fail";
        }
    }

    @GetMapping("/getEnameByTel/{etel}")
    public String getEnameByTel(@PathVariable String etel){
        String ename = emplyeeService.getEnameByTel(etel);
        return ename;
    }

    @GetMapping("/findAll")
    public List<Emplyee> findAll(){
        List<Emplyee> allEmplyee = emplyeeService.getAllEmplyee();
        return allEmplyee;
    }

    @GetMapping("/findByEname/{ename}")
    public Emplyee findByEname(@PathVariable String ename){
        return emplyeeService.findByEname(ename);
    }


    @GetMapping("/getPermissionByUserName/{ename}")
    public List<Permission> getPermissionByUserName(@PathVariable String ename){
        System.out.println(emplyeeService.getPermissionByUserName(ename));
        return emplyeeService.getPermissionByUserName(ename);
    }

    @GetMapping("/getByEnameRole/{ename}")
    public Role getByEnameRole(@PathVariable String ename){
        return emplyeeService.getByEnameRole(ename);
    }

    @PostMapping("/updateRole/{ename}/{rid}")
    public String updateRole(@PathVariable String ename,@PathVariable Integer rid){
        Integer eid = emplyeeService.getByEnameEmplyee(ename);
        try {
            emplyeeService.updateRole(eid,rid);
            return "success";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "fail";
    }

    @GetMapping("/deleEmply/{ename}")
    public String deleEmply(@PathVariable String ename){
        int i = emplyeeService.deleteEmplyee(ename);
        if(i==1){
            return "success";
        }
        return "fail";
    }


    @PostMapping("/updEmplyee")
    public String updEmplyee(@RequestBody Emplyee emplyee){
        try {
            emplyeeService.updEmplyee(emplyee);
            return "success";
        } catch (Exception e) {
            e.printStackTrace();
            return "fail";
        }
    }


    @GetMapping("/getEname/{ename}")
    public String getEname(@PathVariable String ename){
        return ename;
    }

}
